#!/usr/bin/perl -w
# CVS checkout
# vim:tw=100 sw=2 expandtab ft=perl

# Example:
# cvspass => {
#   # This gets installed to /root/.cvspass
#   ':pserver:rollout@cvs.domain:2401/home/cvs' => 'Ahohlasd=',
# },
#
# cvs => {
#   '/tmp/checkout_dir' => {
#     module => 'cvs_module',
#     rev => 'HEAD',
#     repository => ':pserver:rollout@cvs.domain:2401/home/cvs',
#     cmd => 'ls -l /tmp/checkout_dir',
#   },
# },

return unless i_has('cvs');

my %cvspass = flatten_hash(c("$hostname/cvspass"));
while (my($user, $pass) = each %cvspass) {
  file_append(-file => "/root/.cvspass", -add => "/1 $user $pass", -match => qr/ $user /,
              -uid => 0, -gid => 0, -mode => 0600, -create => 1);
}

file_append(-file => "/root/.cvsrc", -add => "cvs -z3 -q", -match => qr/^cvs/, -create => 1);
file_append(-file => "/root/.cvsrc", -add => "update -d -P", -match => qr/^update/);
file_append(-file => "/root/.cvsrc", -add => "checkout -P", -match => qr/^checkout/);
file_append(-file => "/root/.cvsrc", -add => "diff -u -b -B", -match => qr/^diff/);

my %cvs = flatten_hash(c("$hostname/cvs"));
package_check("cvs") if keys %cvs;
while (my($dir, $x) = each(%cvs)) {
  w "No module specified for $dir" or next unless $x->{module};

  cvs_checkout(-dest => $dir, -module => $x->{module}, -rev => $x->{rev}, -repo => $x->{repository},
               -cmd => $x->{post_cmd});
}
